﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq; 
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class BikeAttributesForm : Form
    {
        public BikeAttributesForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 创建包含4个Label的数组
            Label[] labelsToSave = { label1, label2, label3, label4 };

            // 调用保存方法
            SaveLabelsToTextFile(labelsToSave);
        }

        public void SaveLabelsToTextFile(Label[] labels)
        {
            // 参数检查
            if (labels == null || labels.Length == 0)
            {
                MessageBox.Show("未提供有效的Label控件");
                return;
            }

            // 创建保存文件对话框
            SaveFileDialog saveFileDialog = new SaveFileDialog()
            {
                Filter = "文本文件|*.txt",
                Title = "保存Label文本内容",
                DefaultExt = "txt",
                AddExtension = true
            };

            // 显示对话框并获取结果
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    // 使用StreamWriter写入文件
                    using (StreamWriter writer = new StreamWriter(saveFileDialog.FileName))
                    {
                        foreach (Label label in labels)
                        {
                            if (label != null)
                            {
                                // 写入文本并换行
                                writer.WriteLine(label.Text ?? string.Empty);
                            }
                        }
                    }
                    MessageBox.Show("文件保存成功！\n路径：" + saveFileDialog.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("保存失败：\n" + ex.Message);
                }
            }
        }
    }
}
